<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录</title>
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <link rel="stylesheet" href="css/login.css">
    <script type="text/javascript" src="js/jquery.min.js"></script>
</head>

<body>
    <div class="container-login">
        <div class="container-pic">
            <img src="pic/computer.png" width="350px">
        </div>
        <div class="login-dialog">
            <h3>登录</h3>
            <div class="row">
                <span>用户名</span>
                <input type="text" name="userName" id="userName" class="form-control">
            </div>
            <div class="row">
                <span>密码</span>
                <input type="password" name="password" id="password" class="form-control">
            </div>
            <div class="row">
                <span>验证码</span>
                <input type="text" name="inputCaptcha" id="inputCaptcha" class="form-control">
                </div>
            <div class="row">
                <img id="verificationCodeImg" src="/captcha/getCaptcha" style="cursor: pointer;" title="看不清？换一张" />
            </div>
            <div class="row">
                <button type="button" class="btn btn-info btn-lg" onclick="login()">登录</button>
            </div>
        </div>
    </div>
    <script src="js/jquery.min.js"></script>
    <script>
        // 刷新验证码
        $("#verificationCodeImg").click(function(){
            $(this).hide().attr('src', '/captcha/getCaptcha?dt=' + new Date().getTime()).fadeIn();
        });

        function login() {
            var captcha = $("#inputCaptcha").val();
            $.ajax({
                type: "post",
                url: "/captcha/check",
                data: {
                    captcha: captcha
                },
                success: function (result) {
                    if (result) {
                        // 验证码正确，进行登录操作
                        $.ajax({
                            type: "post",
                            url: "/user/login",
                            data: {
                                name: $("#userName").val(),
                                password: $("#password").val()
                            },
                            success: function (result) {
                                if (result!=null && result.data == true) {
                                    // 登录成功
                                    location.href = "book_list.html?currentPage = 1";
                                } else {
                                    alert("账号或密码错误");
                                }
                            }
                        });
                    } else {
                        alert("验证码错误");
                        // 刷新验证码
                        $("#verificationCodeImg").click();
                    }
                }
            });
        }
    </script>
</body>

</html>